/**
 * File:   AddNodeDialog.java
 * Author: Gvennet
 */
package gui.main;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.regex.Pattern;

/**
 * todo write javadoc
 */
public final class AddNodeDialog
{

	private static JTextField ip;

	private static JTextField port;

	private static JDialog dialog;

	private static String answer = null;

	private static void createAddNodeDialog(JFrame owner, String name)
	{
		dialog = new JDialog(owner, name, true);

		dialog.setResizable(false);
		dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
		dialog.addWindowListener(new WindowAdapter()
		{
			@Override
			public void windowClosing(WindowEvent e)
			{
				dialog.dispose();
			}
		});

		JLabel ipLabel = new JLabel("Адрес");
		ip = new JTextField(15);

		JLabel portLabel = new JLabel("Порт");
		port = new JTextField(5);
		port.setText("0");
		port.setAction(new OkAction());


		JButton okButton = new JButton(new OkAction());
		JButton cancelButton = new JButton(new CancelAction());

		GroupLayout layout = new GroupLayout(dialog.getContentPane());
		dialog.getContentPane().setLayout(layout);
		layout.setAutoCreateGaps(true);
		layout.setAutoCreateContainerGaps(true);
		layout.setHorizontalGroup(layout.createParallelGroup()
				.addGroup(layout.createSequentialGroup().addGap(30).addComponent(ipLabel).addComponent(ip).addGap(30))
				.addGroup(layout.createSequentialGroup().addGap(30).addComponent(portLabel).addGap(32).addComponent(port).addGap(30))
				.addGroup(GroupLayout.Alignment.CENTER, layout.createSequentialGroup().addComponent(cancelButton).addComponent(okButton)));
		layout.setVerticalGroup(layout.createSequentialGroup()
				.addGroup(layout.createParallelGroup().addComponent(ipLabel).addComponent(ip))
				.addGroup(layout.createParallelGroup().addComponent(portLabel).addComponent(port))
				.addGap(10)
				.addGroup(layout.createParallelGroup().addComponent(cancelButton).addComponent(okButton)));
		layout.linkSize(SwingConstants.HORIZONTAL, ipLabel, portLabel);
		layout.linkSize(SwingConstants.HORIZONTAL, okButton, cancelButton);
		layout.linkSize(SwingConstants.VERTICAL, ipLabel, ip);
		layout.linkSize(SwingConstants.VERTICAL, portLabel, port);

		dialog.pack();

	}

	public static InetAddress getIpAddress()
	{
		try
		{
			return InetAddress.getByName(ip.getText());
		}
		catch (UnknownHostException e)
		{
			throw new RuntimeException(e); //todo
		}
	}

	public static int getPort()
	{
		return Integer.valueOf(port.getText());
	}

	private static boolean validateAnIp(String ipAddress)
	{
		final Pattern IP_PATTERN =
				Pattern.compile("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}");

		boolean bool = IP_PATTERN.matcher(ipAddress).matches();
		if (bool)
		{
			String[] str = ipAddress.split("\\.");
			for (String aStr : str)
			{
				if (Integer.valueOf(aStr) > 255)
				{
					return false;
				}
			}
		}
		return bool;
	}

	private static boolean validatePort(String port)
	{
		boolean bool = true;
		try
		{
			Integer.valueOf(port);
		}
		catch (NumberFormatException e)
		{
			bool = false;
		}
		return bool;
	}


	public static String showDialog(JFrame owner, String name)
	{
		if (dialog == null)
		{
			createAddNodeDialog(owner, name);
		}

		dialog.setLocationRelativeTo(owner);
		dialog.setVisible(true);
		return answer;
	}


	private static class OkAction extends AbstractAction
	{

		OkAction()
		{
			super("Добавить");
		}

		//@Override

		public void actionPerformed(ActionEvent e)
		{

			if (!validateAnIp(ip.getText()))
			{
				JOptionPane.showMessageDialog(dialog, "Адрес введён неверно", "", JOptionPane.ERROR_MESSAGE);
			}
			else
			{
				if (!validatePort(port.getText()))
				{
					JOptionPane.showMessageDialog(dialog, "Порт введён неверно", "", JOptionPane.ERROR_MESSAGE);
				}
				else
				{
					answer = "Ok";
					dialog.dispose();
				}
			}

		}

	}

	private static class CancelAction extends AbstractAction
	{

		CancelAction()
		{
			super("Отменить");
		}

		//@Override

		public void actionPerformed(ActionEvent e)
		{
			answer = "Cancel";
			dialog.setVisible(false);
		}

	}
}